<template>
	<view>
		<view class="list">
			<picker class="picker" mode="multiSelector" :range="region" range-key="value" :value="regionIndex"
				@change="pickerChange" @columnchange="pickerColumnchange">
				<view class="pbox" :class="{'pbox_hover':regionStr != '请选择省市区'}">
					<view class="texts">{{regionStr}}</view>
				</view>
			</picker>
		</view>
	</view>
</template>

<script>
	import region from '@/common/ChinaCitys.json';
	export default {
		data() {
			return {
				// 原数组
				oldRegion: region,
				// 处理后的数组
				region: [
					[],
					[],
					[]
				],
				// 选择省市区的下标Index  传则默认选中传递的
				regionIndex: [0, 0, 0],
				// 省市区字符串
				regionStr: '请选择省市区',
				previnceId:110000,
				cityId: 110100,
				countyId:110101,
			};
		},
		created() {
			
			this.regionStr = '请选择省市区'
			let provinceArr = [];
			let cityArr = [];
			this.oldRegion.map((item, index) => {
				this.region[0].push({
					value: item.value,
					code: item.code
				});
				if (this.previnceId == item.code) {
					provinceArr = item.children;
					this.regionIndex[0] = index;
				}
			})
			provinceArr.map((item, index) => {
				this.region[1].push({
					value: item.value,
					code: item.code
				});
				if (this.cityId == item.code) {
					cityArr = item.children;
					this.regionIndex[1] = index;
				}
			})
			cityArr.map((item, index) => {
				this.region[2].push({
					value: item.value,
					code: item.code
				});
				if (this.countyId == item.code) {
					this.regionIndex[2] = index;
				}
			})
		},
		methods: {
			pickerChange(e) {
				this.regionIndex = e.detail.value;
				this.regionStr = this.region[0][this.regionIndex[0]].value + '/' + this.region[1][this.regionIndex[1]]
					.value + '/' +
					this.region[2][this.regionIndex[2]].value;
				// 组件传值
				this.$emit('regionStr', this.regionStr)
			},
			pickerColumnchange(e) {
				if (e.detail.column === 0) {
					// 声明城市数组
					let cityArr = [];
					let countyArr = [];
					// 设置下标
					this.regionIndex = [e.detail.value, 0, 0];
					// 改变城市列表
					this.region[1] = this.oldRegion[e.detail.value].children.map(item => {
						cityArr.push({
							value: item.value,
							code: item.code
						});
					})
					this.$set(this.region, 1, cityArr);
					// 改变县区列表
					this.oldRegion[e.detail.value].children[0].children.map(item => {
						countyArr.push({
							value: item.value,
							code: item.code
						});
					})
					this.$set(this.region, 2, countyArr);
				}
				if (e.detail.column === 1) {
					this.regionIndex[1] = e.detail.value;
					this.regionIndex[2] = 0;
					let countyArr = [];
					this.oldRegion[this.regionIndex[0]].children[this.regionIndex[1]].children.map(item => {
						countyArr.push({
							value: item.value,
							code: item.code
						});
					})
					this.$set(this.region, 2, countyArr);
				}
				if (e.detail.column === 2) {
					this.regionIndex[2] = e.detail.value;
				}
			}
		},

	}
</script>

<style lang="scss">
	.list {
		margin: 26rpx auto 0rpx;
		.picker {
			.pbox {
				display: flex;
				align-items: center;
				color: #D1CDCD;
				box-sizing: border-box;
				border-radius: 20rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
			}
			.pbox_hover {
				color: #000000;
			}
		}
	}
</style>